package com.fand.common.util;


import java.math.RoundingMode;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fand.common.exception.BusinessException;
import com.fand.common.model.BaseDTO;

/**
 * @Author: zhangjiabin
 * @Date: 2021/5/26
 * @Version 1.0
 */
public class PageUtil<T extends BaseDTO> {

    public static <T> Page page(T baseDTO){
        if(!(baseDTO instanceof BaseDTO)){
            throw new BusinessException("必须继承 BaseDTO 父类");
        }
        return new Page(((BaseDTO) baseDTO).getPage(),((BaseDTO) baseDTO).getLimit());
    }

    public static PageResult result(Page page){
        return PageResult.builder().data(page.getRecords()).count(Long.valueOf(page.getTotal()).intValue()).build();

    }

    public static PageResult resultToClass(Page page,Class clazz){
        return PageResult.builder().data(JSONUtil.toList(JSONUtil.parseArray(page.getRecords()), clazz)).count(Long.valueOf(page.getTotal()).intValue()).build();
    }

    public static Long scalePages(Long total,Long pageSize){
        return NumberUtil.div(total, pageSize, 0, RoundingMode.UP).longValue();
    }
}
